package com.atguigu.flink.chapter12;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/25 14:36
 */
public class Flink01_TopN {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        //0 动态表与source关联
        tenv.executeSql("create table ub(" +
                            "   user_id bigint, " +
                            "   item_id bigint, " +
                            "   category_id int, " +
                            "   behavior string, " +
                            "   ts bigint," +
                            "   et as to_timestamp(from_unixtime(ts)), " +
                            "   watermark for et as et - interval '3' second " +
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/UserBehavior.csv', " +
                            "   'format' = 'csv' " +
                            ")");
        
        // 1. 按照商品id, 窗口(hop) 分组, 计算点击量
        Table t1 = tenv.sqlQuery("select" +
                                     " item_id, " +
                                     " hop_start(et, interval '30' minute,interval '2' hour) w_start, " +
                                     " hop_end(et, interval '30' minute,interval '2' hour) w_end, " +
                                     " count(*) ct " +
                                     "from ub  " +
                                     "where behavior='pv' " +
                                     "group by item_id, hop(et, interval '30' minute,interval '2' hour)");
        tenv.createTemporaryView("t1", t1);
        
        // 2. 使用over窗口: 按照点击量进行排序, 每个数据添加一个排名(row_number)
        Table t2 = tenv.sqlQuery("select" +
                                     " *, " +
                                     " row_number() over(partition by w_end order by ct desc) rn " +
                                     "from t1");
        tenv.createTemporaryView("t2", t2);
        
        // 3. 使用过滤出来topN where rn <= 3
        Table t3 = tenv.sqlQuery("select " +
                                     " w_end," +
                                     " item_id, " +
                                     " ct item_count, " +
                                     " rn rk " +
                                     "from t2 " +
                                     "where rn <= 3");
        
        // 4. 把结果写入到mysql中 官方建议: 把topN的结果写入到支持更新的数据库中
        tenv.executeSql("CREATE TABLE `hot_item` (\n" +
                            "  `w_end` timestamp,\n" +
                            "  `item_id` bigint,\n" +
                            "  `item_count` bigint,\n" +
                            "  `rk` bigint,\n" +
                            "  PRIMARY KEY (`w_end`,`rk`) NOT ENFORCED\n" +
                            ")with(" +
                            "   'connector' = 'jdbc',\n" +
                            "   'url' = 'jdbc:mysql://hadoop162:3306/flink_sql',\n" +
                            "   'table-name' = 'hot_item', " +
                            "   'username' = 'root', " +
                            "   'password' = 'aaaaaa' " +
                            ")");
    
        t3.executeInsert("hot_item");
        
        
    }
}
